package com.hongkzh.www.look.view.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hongkzh.www.R;
import com.hongkzh.www.look.model.bean.HistoryBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by asad on 2018/3/26.
 * 我的历史观看记录的Adapter
 */

public class HistorySmlvAdapter extends BaseAdapter implements View.OnClickListener {

    private List<HistoryBean.DataBean.ListBean> mHistoryList = new ArrayList<>();
    private HistoryBean.DataBean dataBean = new HistoryBean.DataBean();

    private HistoryBean.DataBean.ListBean mHisBean;

    @Override
    public int getCount() {
        return mHistoryList == null ? 0 : mHistoryList.size();
    }

    @Override
    public Object getItem(int position) {
        return mHistoryList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_hismlv, parent, false);
            new ViewHolder(convertView);
        }

        ViewHolder holder = (ViewHolder) convertView.getTag();
        HistoryBean.DataBean.ListBean bean = mHistoryList.get(position);
        Glide.with(parent.getContext()).load(bean.getHeadImg()).into(holder.IVHeadImg); //设置头像
        Glide.with(parent.getContext()).load(bean.getCoverImgSrc()).into(holder.IVCoverImg); //设置封面
        holder.TvDateTime.setText(bean.getCreateDate()); // 设置媒体时间
        holder.TvHistoryTitle.setText(bean.getTitle()); // 设置标题
        holder.TvMediaName.setText(bean.getName()); //设置媒体名称


        return convertView;
    }


    public void add(HistoryBean historyBean) {
        if (dataBean.getPageNumber() == historyBean.getData().getPageNumber()) {
            mHistoryList = historyBean.getData().getList();
        } else {
            for (int i = 0; i < historyBean.getData().getList().size(); i++) {
                mHistoryList.add(historyBean.getData().getList().get(i));
            }
        }
    }

    public void remove(int position) {
        if (mHistoryList != null) {
            mHistoryList.remove(mHistoryList.get(position));
        }
    }


    public HistoryBean.DataBean.ListBean  getBeanByPosition(int position){
        if(mHistoryList!=null && mHistoryList.size()!=0){
            mHisBean=mHistoryList.get(position);
        }
        return mHisBean;
    }

    public void clearHistoryList() {
        if (mHistoryList != null) {
            mHistoryList.clear();
        }
    }


    @Override
    public void onClick(View v) {
        onItemClickListener.onItemClick();
    }

    public interface onItemClickListener {
        void onItemClick();
    }

    private onItemClickListener onItemClickListener;

    public void setOnItemClickListener(HistorySmlvAdapter.onItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    static class ViewHolder {

        @BindView(R.id.IV_coverImg)
        ImageView IVCoverImg;
        @BindView(R.id.Tv_History_Title)
        TextView TvHistoryTitle;
        @BindView(R.id.IV_HeadImg)
        ImageView IVHeadImg;
        @BindView(R.id.Tv_Media_Name)
        TextView TvMediaName;
        @BindView(R.id.Tv_dateTime)
        TextView TvDateTime;
        @BindView(R.id.layout_history_item)
        RelativeLayout layoutHistoryItem;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
            view.setTag(this);
        }
    }
}
